package com.one.controller;

import com.one.pojo.OneUser;
import com.one.pojo.Store;
import com.one.pojo.SysUser;
import com.one.pojo.UserTrasRecord;
import com.one.service.OneUserService;
import com.one.service.StoreService;
import com.one.service.SysUserService;
import com.one.service.UserTrasRecordService;
import com.one.util.JSONMsg;
import com.one.util.JSONResult;
import com.one.util.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author semstouch
 * @Date 2017/5/14
 **/
@Controller
@RequestMapping("/web")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private OneUserService oneUserService;
    @Resource
    private StoreService storeService;
    @Resource
    private UserTrasRecordService userTrasRecordService;
    /**
     * 跳转到设登陆页面方法
     * @return
     */
    @RequestMapping(value="/toLogin")
    public ModelAndView toLogin(){
        ModelAndView modelAndView=new ModelAndView("login");
        return modelAndView;
    }

    /**
     * 跳转到用户列表页面方法
     * @return
     */
    @RequestMapping(value="/toUser")
    public ModelAndView toUser(){
        ModelAndView modelAndView=new ModelAndView("user");
        return modelAndView;
    }

    /**
     * 跳转到小鸡交易列表页面方法
     * @return
     */
    @RequestMapping(value="/toChickTransaction")
    public ModelAndView toChickTransaction(){
        ModelAndView modelAndView=new ModelAndView("chickTransaction");
        return modelAndView;
    }

    /**
     * 跳转到小鸡交易列表页面方法
     * @return
     */
    @RequestMapping(value="/toChickActive")
    public ModelAndView toChickActive(){
        ModelAndView modelAndView=new ModelAndView("chickActive");
        return modelAndView;
    }

    /**
     * 跳转到鸡蛋交易列表页面方法
     * @return
     */
    @RequestMapping(value="/toEggTransaction")
    public ModelAndView toEggTransaction(){
        ModelAndView modelAndView=new ModelAndView("eggTransaction");
        return modelAndView;
    }



    /**
     *登出跳转到登陆页面方法
     * @param request
     * @return
     */
    @RequestMapping(value = "/toLoginOut")
    public ModelAndView toLoginOut(HttpServletRequest request) {
        ModelAndView modelAndView=new ModelAndView("login");
        request.getSession().setAttribute("sysuser",null);
        return modelAndView;
    }

    @RequestMapping("/activeData")
    @ResponseBody
    public JSONMsg activeData(HttpServletRequest request,UserTrasRecord e){
        JSONMsg jsonResult = new JSONMsg();
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if(pageSize < 0){
            pageSize = 10;
        }
        e.setGiftType("3");
        jsonResult.setData(userTrasRecordService.selectPageList(offset,pageSize,e));
        jsonResult.setTotal(userTrasRecordService.selectPageCount(e));
        return jsonResult;
    }



    @RequestMapping("/chickData")
    @ResponseBody
    public JSONMsg chickData(HttpServletRequest request,UserTrasRecord e){
        JSONMsg jsonResult = new JSONMsg();
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if(pageSize < 0){
            pageSize = 10;
        }
        e.setGiftType("1");
        jsonResult.setData(userTrasRecordService.selectPageList(offset,pageSize,e));
        jsonResult.setTotal(userTrasRecordService.selectPageCount(e));
        return jsonResult;
    }

    @RequestMapping("/eggData")
    @ResponseBody
    public JSONMsg eggData(HttpServletRequest request, UserTrasRecord e){
        JSONMsg jsonResult = new JSONMsg();
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if(pageSize < 0){
            pageSize = 10;
        }
        e.setGiftType("2");
        jsonResult.setData(userTrasRecordService.selectPageList(offset,pageSize,e));
        jsonResult.setTotal(userTrasRecordService.selectPageCount(e));
        return jsonResult;
    }

    @RequestMapping("/userData")
    @ResponseBody
    public JSONMsg userData(HttpServletRequest request, SysUser e){
        JSONMsg jsonResult = new JSONMsg();
        int offset = 0;
        int pageSize = 10;
        if (request.getParameter("start") != null) {
            offset = Integer.parseInt(request.getParameter("start"));
        }
        if (request.getParameter("length") != null) {
            pageSize = Integer.parseInt(request.getParameter("length"));
        }
        if (offset < 0)
            offset = 0;
        if(pageSize < 0){
            pageSize = 10;
        }
        jsonResult.setData(oneUserService.selectPageList(offset,pageSize));
        jsonResult.setTotal(oneUserService.selectPageCount());
        return jsonResult;
    }

    @RequestMapping(value = "/store/query", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult storeQuery(Store store,HttpServletRequest request){
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(storeService.findByUserId(store.getUserId()));
        return  jsonResult;
    }

    @RequestMapping(value = "/store/edit", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult storeEdit(Store store,HttpServletRequest request){
        JSONResult jsonResult = new JSONResult();
        storeService.update(store);
        return  jsonResult;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult login(SysUser sysUser,HttpServletRequest request){
        JSONResult jsonResult = new JSONResult();
        SysUser rs= sysUserService.selectByLoginName(sysUser);

        if(rs==null){
            jsonResult.setCode("-1");
            jsonResult.setMsg("用户不存在！");
            return jsonResult;
        }
        if(!rs.getPwd().equals(MD5.md5(sysUser.getPwd()))){
            jsonResult.setCode("-1");
            jsonResult.setMsg("用户或密码错误！");
            return jsonResult;
        }
        request.getSession().setAttribute("sysuser",rs);
        jsonResult.setCode("1");
        return jsonResult;
    }
}
